package com.batchbrake.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;

import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXStatusBar;
import org.jdesktop.swingx.painter.CompoundPainter;
import org.jdesktop.swingx.painter.GlossPainter;
import org.jdesktop.swingx.painter.MattePainter;
import org.jdesktop.swingx.painter.Painter;
import org.jdesktop.swingx.painter.TextPainter;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.HorizontalAlignment;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.VerticalAlignment;

import com.batchbrake.buttons.ProcessButton;
import com.batchbrake.progress.ProgressEvent;
import com.batchbrake.queue.QueueBuilder;
import com.jhlabs.image.ShadowFilter;


public class BatchBrakeFrame extends JXFrame implements Observer, ActionListener, WindowListener {
	
	private BatchBrakeProc proc = null;
	private JXStatusBar statusBar = null;
	private JProgressBar progressBar = null;
	
	private ProcessButton btnStartEncoding = null;
	private QueueBuilder builder = null;
	private BatchBrakePanel panel = null;
	public JXLabel lblOutputFolder = null;
	public JXLabel lblPreset = null;
	
	public BatchBrakeFrame(QueueBuilder builder, BatchBrakeProc proc) {
		this.proc = proc;
		this.builder = builder;
		this.initComponents();
	}

	
	private void initComponents() {
		MattePainter mp = new MattePainter(new Color(0.5f,0.8f,0.4f,0.5f));
		GlossPainter gp = new GlossPainter(new Color(1f, 1f, 1f, 0.3f), GlossPainter.GlossPosition.TOP);
		Painter p = new CompoundPainter(mp, gp);

		ShadowFilter shadow = new ShadowFilter();
		shadow.setShadowColor(Color.black.getRGB());
		System.out.println("shadow: " + shadow.getShadowColor());
		shadow.setDistance(1.0f);
		shadow.setRadius(5.0f);
		shadow.setOpacity(0.7f);

		TextPainter tp = new TextPainter("BatchBrake");
		tp.setAntialiasing(true);
		tp.setHorizontalAlignment(HorizontalAlignment.LEFT);
		tp.setVerticalAlignment(VerticalAlignment.CENTER);
		tp.setFilters(shadow);

		tp.setFillPaint(Color.white);
		
		JXPanel header = new JXPanel(new BorderLayout());
		header.setBackgroundPainter(p);
		
		JXLabel icn = new JXLabel();
		icn.setIcon(new ImageIcon("images/BatchBrake-48x48.png"));
		icn.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		header.add(icn, BorderLayout.WEST);
		
		JXPanel info = new JXPanel(new BorderLayout());
		info.setOpaque(false);
		
		JXLabel lbl = new JXLabel();
		lbl.setForegroundPainter(tp);
		lbl.setFont(lbl.getFont().deriveFont(Font.BOLD, 25));
		lbl.setBorder(BorderFactory.createEmptyBorder(5, 10, 0, 0));
		
		Box boxInfos = new Box(BoxLayout.Y_AXIS);
		lblOutputFolder = new JXLabel("<html><b>Output folder : </b>" + BatchBrake.outputFolder + "</html>");
//		lblOutputFolder.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 0));
		lblOutputFolder.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		
		lblPreset = new JXLabel("<html><b>Preset : </b>" + "Normal" + "</html>");
		lblPreset.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 0));
		boxInfos.add(lblOutputFolder);
		boxInfos.add(lblPreset);
		
		info.add(lbl, BorderLayout.CENTER);
		info.add(boxInfos, BorderLayout.SOUTH);
		header.add(info, BorderLayout.CENTER);
		header.setPreferredSize(new Dimension(200, 70));
		
		btnStartEncoding = new ProcessButton("Start encoding");
		btnStartEncoding.addActionListener(this);
		proc.addObserver(btnStartEncoding);
		
//		btnStartEncoding.setPreferredSize(new Dimension(100, 18));
//		btnStartEncoding.setMinimumSize(new Dimension(100, 18));
//		btnStartEncoding.setMaximumSize(new Dimension(100, 18));
//		btnStartEncoding.setSize(100,18);
//		btnStartEncoding.setBorder(BorderFactory.createLineBorder(Color.red));
		header.add(btnStartEncoding, BorderLayout.EAST);
//		tabs = new JTabbedPane();

		
//		toolbar = new JToolBar();
//		toolbar.add(btnStartEncoding);
//
		progressBar = new JProgressBar();
//		
		JXStatusBar.Constraint c2 = new JXStatusBar.Constraint(JXStatusBar.Constraint.ResizeBehavior.FILL);
//
		statusBar = new JXStatusBar();
		statusBar.add(new JXLabel(), c2);
		statusBar.add(progressBar);
//		
//		
//		
//		jobsPanel = new JobsPanel(proc);
//	
		panel = new BatchBrakePanel(this, proc, builder);
//		
		this.setLayout(new BorderLayout());
		this.add(header, BorderLayout.NORTH);
		this.add(panel, BorderLayout.CENTER);
		
		this.setSize(800, 700);
		this.setStartPosition(StartPosition.CenterInScreen);
		this.addWindowListener(this);
		this.addWindowListener(this);
		this.setStatusBar(statusBar);
		
		this.setTitle("BatchBrake -- An easy queue editor for HandBrake");
	}


	@Override
	public void update(Observable obs, Object obj) {
		if( obs instanceof QueueBuilder ) {
			if( obj instanceof ProgressEvent ) {
				progressBar.setMinimum(((ProgressEvent) obj).getMin());
				progressBar.setMaximum(((ProgressEvent) obj).getMax());
				progressBar.setValue(((ProgressEvent) obj).getValue());

				if( ((ProgressEvent) obj).getValue() == ((ProgressEvent) obj).getMax() ) {
					proc.addToQueue(((QueueBuilder)obs).getQueue());
					builder.clearAll();
					progressBar.setValue(0);
//					progressBar.setVisible(false);
				} else if( !progressBar.isVisible() ) {
//					progressBar.setValue(0);
//					progressBar.setVisible(true);
				}
			}
		}
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if( e.getSource().equals(btnStartEncoding) ) {
			if( btnStartEncoding.isProcessLaunched() ) {
				proc.stopAll();
				btnStartEncoding.setProcessLaunched(false);
				panel.setTimerEnabled(false);
			} else {
				btnStartEncoding.setProcessLaunched(true);
				panel.setTimerEnabled(true);
				
				proc.setPreset((String)panel.cboPresets.getSelectedItem());
				new Thread(proc).start();
			}
		}
	}


	/**
	 * @name WindowListener methods implementation
	 */
	@Override
	public void windowActivated(WindowEvent e) {}
	@Override
	public void windowClosed(WindowEvent e) {}
	@Override
	public void windowClosing(WindowEvent e) {
		proc.stopAll();
		System.exit(0);
	}
	@Override
	public void windowDeactivated(WindowEvent e) {}
	@Override
	public void windowDeiconified(WindowEvent e) {}
	@Override
	public void windowIconified(WindowEvent e) {}
	@Override
	public void windowOpened(WindowEvent e) {}
}
